package com.webinsky.article.persist.dao;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectKey;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.type.JdbcType;

import com.webinsky.article.persist.entity.ArticleRelateVideo;

public interface ArticleRelateVideoMapper {
	
	@Delete({
		"delete from article_relate_video where article_id = #{articleId, jdbcType=INTEGER}"
	})
	int deleteByArticleId(@Param("articleId")int articleId);
	
	@Delete({
		"delete from article_relate_video where relate_video_id = #{videoId, jdbcType=INTEGER}"
	})	
	int deleteByVideoId(@Param("videoId")int videoId);
	
	 @Insert({
		"insert into article_relate_video (article_id, relate_video_id, sort, del, create_at, update_at, language)",
		"values (#{articleId,jdbcType=INTEGER}, #{relateVideoId,jdbcType=INTEGER}, #{sort,jdbcType=INTEGER}, #{del,jdbcType=INTEGER}, ",
		"now(), now(), #{language,jdbcType=VARCHAR})"
	})
	@SelectKey(statement="SELECT LAST_INSERT_ID()", keyProperty="id", before=false, resultType=Integer.class)
	int insert(ArticleRelateVideo record);
	    
	@Select({
	    "select ",
	    "id, article_id, relate_video_id, sort, del, create_at, update_at, language",
	    "from article_relate_video",
	    "where del = 0"
	})
	@Results({
	    @Result(column="id", property="id", jdbcType=JdbcType.INTEGER),
	    @Result(column="article_id", property="articleId", jdbcType=JdbcType.INTEGER),
	    @Result(column="relate_video_id", property="relateVideoId", jdbcType=JdbcType.VARCHAR),
	    @Result(column="sort", property="sort", jdbcType=JdbcType.INTEGER),
	    @Result(column="del", property="del", jdbcType=JdbcType.INTEGER),
	    @Result(column="create_at", property="createAt", jdbcType=JdbcType.TIMESTAMP),
	    @Result(column="update_at", property="updateAt", jdbcType=JdbcType.TIMESTAMP),
	    @Result(column="language", property="language", jdbcType=JdbcType.VARCHAR)
	})
	List<ArticleRelateVideo> selectActiveAll();
	
	@Select({
	    "select ",
	    "id, article_id, relate_video_id, sort, del, create_at, update_at, language",
	    "from article_relate_video",
	    "where article_id = #{articleId, jdbcType=INTEGER} and del = #{del, jdbcType=INTEGER}"
	})
	@Results({
	    @Result(column="id", property="id", jdbcType=JdbcType.INTEGER),
	    @Result(column="article_id", property="articleId", jdbcType=JdbcType.INTEGER),
	    @Result(column="relate_video_id", property="relateVideoId", jdbcType=JdbcType.VARCHAR),
	    @Result(column="sort", property="sort", jdbcType=JdbcType.INTEGER),
	    @Result(column="del", property="del", jdbcType=JdbcType.INTEGER),
	    @Result(column="create_at", property="createAt", jdbcType=JdbcType.TIMESTAMP),
	    @Result(column="update_at", property="updateAt", jdbcType=JdbcType.TIMESTAMP),
	    @Result(column="language", property="language", jdbcType=JdbcType.VARCHAR)
	})
	List<ArticleRelateVideo> selectByArticleId(@Param("articleId")int articleId, @Param("del")int del);
	
	@Select({
		"select ",
	    "id, article_id, relate_video_id, sort, del, create_at, update_at, language",
	    "from article_relate_video ",
	    "where id = #{id, jdbcType=INTEGER}"
	})	
	ArticleRelateVideo selectById(@Param("id")int id);
	
	@SelectProvider(type = ArticleRelateVideoMapperProvider.class, method = "selectByArticleIds")
	@Results({
	    @Result(column="id", property="id", jdbcType=JdbcType.INTEGER),
	    @Result(column="article_id", property="articleId", jdbcType=JdbcType.INTEGER),
	    @Result(column="relate_video_id", property="relateVideoId", jdbcType=JdbcType.VARCHAR),
	    @Result(column="sort", property="sort", jdbcType=JdbcType.INTEGER),
	    @Result(column="del", property="del", jdbcType=JdbcType.INTEGER),
	    @Result(column="create_at", property="createAt", jdbcType=JdbcType.TIMESTAMP),
	    @Result(column="update_at", property="updateAt", jdbcType=JdbcType.TIMESTAMP),
	    @Result(column="language", property="language", jdbcType=JdbcType.VARCHAR)
	})
	List<ArticleRelateVideo> selectByArticleIds(@Param("articleIds")List<Integer> ids);
	
	@Select({
		"select ",
	    "id, article_id, relate_video_id, sort, del, create_at, update_at, language",
	    "from article_relate_video ",
	    "where article_id = #{articleId, jdbcType=INTEGER} and del = #{del, jdbcType=INTEGER}"
	})
	@Results({
		@Result(column="id", property="id", jdbcType=JdbcType.INTEGER),
	    @Result(column="article_id", property="articleId", jdbcType=JdbcType.INTEGER),
	    @Result(column="relate_video_id", property="relateVideoId", jdbcType=JdbcType.VARCHAR),
	    @Result(column="sort", property="sort", jdbcType=JdbcType.INTEGER),
	    @Result(column="del", property="del", jdbcType=JdbcType.INTEGER),
	    @Result(column="create_at", property="createAt", jdbcType=JdbcType.TIMESTAMP),
	    @Result(column="update_at", property="updateAt", jdbcType=JdbcType.TIMESTAMP),
	    @Result(column="language", property="language", jdbcType=JdbcType.VARCHAR)
	})
	List<ArticleRelateVideo> selectByArticleIdAndDel(@Param("articleId")int articleId, @Param("del")int del);	
	
	@Update({
	     "update article_relate_video",
	     "set article_id = #{articleId,jdbcType=INTEGER},",
	     "relate_video_id = #{relateVideoId,jdbcType=VARCHAR},",
	     "sort = #{sort,jdbcType=INTEGER},",
	     "del = #{del,jdbcType=INTEGER},",	
	     "create_at = #{createAt, jdbcType=TIMESTAMP},",
	     "update_at = #{updateAt,jdbcType=TIMESTAMP},",
	     "language = #{language,jdbcType=VARCHAR}",
	     "where Id = #{id,jdbcType=INTEGER}"
	 })
	 int updateByPrimaryKey(ArticleRelateVideo record);
}
